package com.starryx.driverschool.service.impl;

import com.starryx.driverschool.dao.StudentRepository;
import com.starryx.driverschool.po.Student;
import com.starryx.driverschool.service.StudentService;
import com.starryx.driverschool.utils.MyBeanUtils;
import com.starryx.driverschool.vo.StudentQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentRepository studentRepository;

    @Override
    public List<Student> listStudent() {
        return studentRepository.findAll();
    }

    @Override
    public Page<Student> listStudent(Pageable pageable) {
        return studentRepository.findAll(pageable);
    }

    @Override
    public Page<Student> listStudent(Pageable pageable, StudentQuery studentQuery) {
        return studentRepository.findAll(new Specification<Student>() {
            @Override
            public Predicate toPredicate(Root<Student> root,       // 相当于表名
                                         CriteriaQuery<?> cq,   // 条件查询执行器
                                         CriteriaBuilder cb) {  // 条件语句构建器，用于定义查询条件
                List<Predicate> predicates = new ArrayList<>();     // 查询条件组
                if (!"".equals(studentQuery.getName()) && studentQuery.getName() != null) {
                    predicates.add(cb.like(root.<String>get("name"), "%" + studentQuery.getName() + "%"));
                }
                cq.where(predicates.toArray(new Predicate[predicates.size()]));     // 将条件构造器构建的查询条件放到执行器执行
                return null;
            }
        }, pageable);
    }

    @Transactional
    @Override
    public Student saveStudent(Student student) {
        student.setUpdateTime(new Date());
        student.setCreateTime(new Date());
        return studentRepository.save(student);
    }

    @Override
    public Student getStudent(Long id) {
        return studentRepository.findOne(id);
    }

    // 根据编号查询
    @Override
    public Student getStudentByNo(String no) {
        return studentRepository.findByNo(no);
    }

    @Transactional
    @Override
    public Student updateStudent(Long id, Student student) {
        Student s = studentRepository.findOne(id);
        if(s == null){
            throw new RuntimeException("不存在该类型");
        }
        BeanUtils.copyProperties(student, s, MyBeanUtils.getNullPropertNames(student));
        s.setUpdateTime(new Date());
        return studentRepository.save(s);
    }

    @Transactional
    @Override
    public void deleteStudent(Long id) {
        studentRepository.delete(id);
    }
}
